#include "GameObjHero.h"

#define PTM_RATIO 32

GameObjHero::GameObjHero(void)
{
	horiSpeed = 2.0f;
	vertSpeed = 0;
	jumpSpeed = 2.0f;
	rightMove = 0;
	leftMove = 0;
	upMove = 0;
	downMove = 0;
	jumpMove = 0;
	moveState = 0;
	isMoving = false;
	gameObjType = HERO;
	isFlight = false;
}


GameObjHero::~GameObjHero(void)
{
}

GameObjHero* GameObjHero::create(const char *pszFileName, const CCPoint &point){
	
	GameObjHero *hero = new GameObjHero();
	if(hero){
		hero->autorelease();
	}else{
		CC_SAFE_DELETE(hero);
		return NULL;
	}

	// initial hero's sprite.
	CCSprite *pobSprite = new CCSprite();
    if (pobSprite && pobSprite->initWithFile(pszFileName))
    {
		pobSprite->setAnchorPoint(ccp(0.5f, 0));
		pobSprite->setPosition(point);
        pobSprite->autorelease();
		hero->sprite = pobSprite;
    }else{
		CC_SAFE_DELETE(pobSprite);
		CC_SAFE_DELETE(hero);
		return NULL;
	}

    return hero;
}


void GameObjHero::keyNormalKeyDown(int keyCode){
	switch(keyCode){
	case VK_KEY_A:{
			this->leftMove = 1;
			if(!isMoving){
				CCTexture2D *texture2 = CCTextureCache::sharedTextureCache()->addImage("zy_body.png");
				CCArray *splitAniframe2 = CCArray::createWithCapacity(18);
				for(int i = 0; i < 4; i++){
					CCSpriteFrame *frame = CCSpriteFrame::createWithTexture(texture2, CCRectMake(i * 65, 0, 65, 33));
					splitAniframe2->addObject(frame);
				}
				CCAnimation *splitAnimation2 = CCAnimation::createWithSpriteFrames(splitAniframe2, 0.1f);
				CCAnimate *splitAnimate2 = CCAnimate::create(splitAnimation2);
				CCActionInterval * orbitcamera = CCOrbitCamera::create(1, 0.5f, 0, 0, 180, 0, 0);
				sprite->runAction(orbitcamera);
				isMoving = true;
			}
			break;
		}
	case VK_KEY_S:
		break;
	case VK_KEY_D:{
			this->rightMove = 1;
			if(!isMoving){
				CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("zy_body_1.png");
				CCArray *splitAniframe = CCArray::createWithCapacity(18);
				for(int i = 0; i < 4; i++){
					CCSpriteFrame *frame = CCSpriteFrame::createWithTexture(texture, CCRectMake(i * 65, 0, 65, 33));
					splitAniframe->addObject(frame);
				}
				CCAnimation *splitAnimation = CCAnimation::createWithSpriteFrames(splitAniframe, 0.1f);
				CCAnimate *splitAnimate = CCAnimate::create(splitAnimation);
				sprite->runAction(CCRepeatForever::create(splitAnimate));
				isMoving = true;
			}
		break;
		}
	case VK_KEY_W:
		break;
	case VK_KEY_1:
		break;
	case VK_KEY_2:
		break;
	case VK_KEY_3:
		break;
	case VK_KEY_4:
		break;
	case VK_SPACE:
		if(moveState&TER_GROUND)
			this->jumpMove = 4;
		break;
	}
}

	void GameObjHero::keyNormalKeyUp(int keyCode){
	switch(keyCode){
	case VK_KEY_A:
		//this->sprite->stopAllActions();
		isMoving = false;
		this->leftMove = 0;
		break;
	case VK_KEY_S:
		break;
	case VK_KEY_D:
		this->sprite->stopAllActions();
		isMoving = false;
		this->rightMove = 0;
		break;
	case VK_KEY_W:
		break;
	case VK_KEY_1:
		break;
	case VK_KEY_2:
		break;
	case VK_KEY_3:
		break;
	case VK_KEY_4:
		break;
	case VK_SPACE:
		if(jumpMove > 0)
			jumpMove = 0;
		break;
	}
}